package com.kanshu.ksgb.fastread.doudou.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.kanshu.ksgb.fastread.doudou.R;

/**
 * Created by wcy on 2017\7\11 0011.
 */
public class BorderImageView extends ImageView {

    private Paint mPaint;
    private int mBorderColor = Color.LTGRAY;

    public BorderImageView(Context context) {
        super(context);
        init();
    }

    public BorderImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BorderImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyleAttr, 0);
        mBorderColor = a.getColor(R.styleable.BorderImageView_border_color, mBorderColor);
        a.recycle();
        init();
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
       solveOne(canvas);
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(mBorderColor);
    }

    private void solveOne(Canvas canvas) {
        // 画边框
        Rect rect1 = getRect(canvas);
        mPaint.setStyle(Paint.Style.STROKE);
        // 画边框
        canvas.drawRect(rect1, mPaint);
        mPaint.setColor(Color.LTGRAY);
        // 画一条竖线,模拟右边的阴影
        canvas.drawLine(rect1.right + 1, rect1.top + 2, rect1.right + 1,
                rect1.bottom + 2, mPaint);
        // 画一条横线,模拟下边的阴影
        canvas.drawLine(rect1.left + 2, rect1.bottom + 1, rect1.right + 2,
                rect1.bottom + 1, mPaint);
        // 画一条竖线,模拟右边的阴影
        canvas.drawLine(rect1.right + 2, rect1.top + 3, rect1.right + 2,
                rect1.bottom + 3, mPaint);
        // 画一条横线,模拟下边的阴影
        canvas.drawLine(rect1.left + 3, rect1.bottom + 2, rect1.right + 3,
                rect1.bottom + 2, mPaint);
    }

    Rect getRect(Canvas canvas) {
        Rect rect = canvas.getClipBounds();
        rect.bottom -= getPaddingBottom();
        rect.right -= getPaddingRight();
        rect.left += getPaddingLeft();
        rect.top += getPaddingTop();
        return rect;
    }

}
